//config的设置是全局的
layui.config({
    base: '/js/plugins/' // 自定义扩展插件所在的目录
}).extend({ //设定模块别名
    yjsloading: 'yjsloading',
    yjslayer: 'yjslayer',
    yjsupload: 'yjsupload',
});

layui.use(['form', 'yjsloading', 'yjsupload','layedit','yjslayer'], function () {
    var $ = layui.jquery
        , form = layui.form
        , yjsupload = layui.yjsupload
        , yjsloading = layui.yjsloading
        , yjslayer = layui.yjslayer
        , layedit = layui.layedit;

    //创建一个编辑器
    var editIndex = layedit.build('student_editor', {
        tool: ['image'],
        uploadImage: {url: '/upload/excellent/image', type: 'post'}
    });

    // 自定义表单验证规则
    form.verify({
        title: function (value) {
            if (!/^[\S]{1,150}$/.test(value)) {
                return '标题长度需在1~150个字符之间，且不能出现空格！';
            }
        },
        content: function (value) {
            layedit.sync(editIndex);
            var contentVal = layedit.getContent(editIndex);
            if (contentVal.length < 10) {
                return '公告内容不得小于10个字符！';
            }
        }
    });

    // 重新获取验证码
    $('.getLVcode').click(function () {
        var url = '/vcode/path/form';
        yjsloading.loadingCaptcha(url);
    });

    // 监听部门信息表单确定按钮事件
    form.on('submit(toolbarEvent)', function (data) {
        $title = '优秀学子信息维护';
        $url = '/excellent/students/add';
        $msg = '您确定执行本次数据操作吗?';
        var content= data.field.content;
        var $temp = $(content);
        result = contentToJson($temp);
        if (result.length == 0) {
            result = [{
                type: 'text',
                content: content
            }];
        }
        data.field.content = result;
        $data = data.field;
        var avatarHtml = $("#avatarViewer").find('img');
        $avatar_src  = avatarHtml.attr("src");
        if ($avatar_src.length == 0) {
            yjslayer.failMsg('请上传头像');
        }else  {
            $data['avatar'] =  $avatar_src;
            yjsloading.ajaxConfirm($title, $msg, $url, $data, function (result) {
                $('#rspStatus').val(result['rspStatus']);
                $('#rspMsg').val(result['rspMsg']);
            });
        }
    });

    // 上传头像事件
    $elem = '#avatar';
    $url = '/upload/excellent/avatar';
    $avatar_data = {flag: 0};
    yjsupload.uploadExcellentAvatar($elem,$url,$avatar_data,function (result) {
        if (result['code'] == 0) {
            $data = result['data'];
            var avatarHtml = $("#avatarViewer").find('img');
            avatarHtml.attr('layer-src', $data['src']);
            avatarHtml.attr('src', $data['src']);
            yjslayer.okMsg(result['msg']);
        }else  {
            yjslayer.failMsg(result['msg']);
        }
    });

    function contentToJson($dom) {
        var content = [];
        for (var i of $dom) {
            var item = [];
            switch (i.tagName) {
                case 'P':
                    if ($(i).has('img').length > 0) {
                        for( var img of $(i).find('img')) {
                            _img = {
                                type: 'image',
                                content: $(img).attr('src')
                            };
                            item.push(_img);
                        }
                        break;
                    }
                    temp_content = $(i).text().trim()
                    if (temp_content.length>0) {
                        item = [{
                            type: 'text',
                            content:temp_content
                        }];
                    }
                    break;
                default:
            }
            if ($(i).children().length > 0) {
                item.children = contentToJson($(i).children())
            } else {
                item.children = [];
            }
            content = content.concat(item)
        }
        return content;
    }
});